<?php

require_once("login/src/Model/UserInfo.php");
require_once("Database/Model/DatabaseConnection.php");

class UserInfoDAL 
{

    /**
    *@return array() userlist 
    **/
	public function getUserList()
	{
		$sql = "SELECT `UserID`, Email, Username, Password FROM UserInfo";

		return $this->UserList($sql);	
	}

    /**
    *@param string
    *@throws new exceptions
    *@return array() userInfo
    **/
	private function UserList($sql)
	{
		$mysqli = new mysqli(   DatabaseConnection::getDatabaseServer(), 
                                DatabaseConnection::getDatabaseUser(), 
                                DatabaseConnection::getdatabasePassword(), 
                                DatabaseConnection::getDatabaseName(), 
                                DatabaseConnection::getConnectionInt());
            
		$stmt = $mysqli->prepare($sql);
        
        if ($stmt == FALSE) 
        {
        	throw new Exception($sql);
        }

        $result = $stmt->execute();
        if ($result == FALSE) 
        {
        	throw new Exception("execute of [$sql] failed " . $stmt->error);
        }

        $result = $stmt->bind_result($userID, $email, $username, $password);
        if ($result == FALSE)
        {
        	throw new Exception("execute of [$sql] failed " . $stmt->error);
        }

        $userinfoList = array();

        while ($stmt->fetch()) 
        {
            $userinfoList[] = UserInfo::CreateUserInfoFromDatabase($userID,
            													   $email, 
            													   $username, 
            													   $password);
        }

        $mysqli->close();
        return $userinfoList;
	}

    /**
    *@param UserInfo
    *@throws new exceptions
    **/
	public function saveNewUser(UserInfo $userinfo)
	{
        $mysqli = new mysqli(   DatabaseConnection::getDatabaseServer(), 
                                DatabaseConnection::getDatabaseUser(), 
                                DatabaseConnection::getdatabasePassword(), 
                                DatabaseConnection::getDatabaseName(), 
                                DatabaseConnection::getConnectionInt());

		$sql = "INSERT INTO UserInfo 
                        (
                        		UserID,
                                Email, 
                                Username, 
                                Password
                        ) 
                        VALUES(?, ?, ?, ?)";

        $statement = $mysqli->prepare($sql);
        if ($statement === FALSE) 
        {
            throw new Exception("prepare of $sql failed " . $this->mysqli->error);
        }


        $userID = $userinfo->getUserID();
	    $email = $userinfo->getEmail();
        $username = $userinfo->getUsername(); 
        $password = $userinfo->getPassword();

        if ($statement->bind_param("isss", $userID,
        								   $email, 
                                           $username, 
                                           $password) === FALSE) 
        {
        }

        if ($statement->execute() === FALSE) 
        {
                throw new Exception("execute of $sql failed " . $statement->error);
        }
		$mysqli->close();
	}

}

